


$(document).on('pageshow','#addproduct', function(event) {
    /**
     * Created by trisatria on 2/21/14.
     */
   /* $('#price').keyup(function() {
        var $th = $(this);
        $th.val( $th.val().replace(/[^a-zA-Z0-9]/g,''));
    });*/
    //init location
    for(var i=0; i< _topic_loc.length ; i = parseInt(i) + 1){
        var content = '<option value="'+i+'" >'+_topic_loc[i]+'</option>';
        if(i == 0){
            $('#topic_location').parent().children('span').text(_topic_loc[i]);
            content = '<option value="'+i+'" selected >'+_topic_loc[i]+'</option>';
        }
        $('#topic_location').append(content);

    }
    //init cat
    for(var i=0; i< _topic_cat.length ; i = parseInt(i) + 1){

        var content = '<option value="'+_topic_cat[i]['id']+'" >'+_topic_cat[i]['title']+'</option>';
        if(_topic_cat[i]['id']== _topic_cat.length ){
            $('#cat_id').parent().children('span').text( _topic_cat[i]['title']);
            content = '<option value="'+_topic_cat[i]['id']+'" selected >'+_topic_cat[i]['title']+'</option>';
        }
        $('#cat_id').append(content);
    }

    //upload product form
    /////click btn upload
    $('#btn_upload_product').click(function(){
        var my_user = user_id;
        var title = $('#title').val();
        var price = $('#price').toNumber().val().replace('.','');
        var expire_date = $('#date').val();
        var topic_location =  $('#topic_location').find(":selected").val();
        var cat_id =  $('#cat_id').find(":selected").val();
        var short_description = $('#short_description').val();
        var full_description = $('#full_description').val();
        var topic_type = $('input[name=topic_type]:checked').val();
        var show_price = $('input[name=show_price]:checked').val();
        var is_brandnew = $('input[name=is_brandnew]:checked').val();
        var images = $('#images').val();
        $.ajax({
            url : _host+"topic/add",
            type: "POST",
            data: {title: title,
                price: price,
                //expire_date: expire_date,
                topic_location: topic_location,
                cat_id: cat_id,
                short_description: short_description,
                full_description: full_description,
                topic_type: topic_type,
                show_price: show_price,
                is_brandnew: is_brandnew,
                user_id: my_user,
                images: '/upload/image/'+images,
                thumb:  '/upload/image/'+images
            }
        }).done(function(data){
                if(data != -1){
                    //$('#alert-sucess').show();
                    var item_array = new Array();
                    $( ".gel_item" ).each(function() {
                        //console.log($(this).attr('src'));
                        //alert($(this).parent().hasClass('thumb_border'));
                            var item_data = {};
                            item_data['topic_id'] = data;
                            item_data['user_id'] = user_id;
                            item_data['link'] =  $(this).attr('src');
                            item_data['link'] = item_data['link'].replace(_host_img,'');
                            item_array.push(item_data);
                    });
                    console.log(JSON.stringify(item_array));
                    $.ajax({
                        url : _host+"topic/addgellary",
                        type: "POST",
                        data: {data: JSON.stringify(item_array)}
                     });
                    $.mobile.changePage('productdetail.html?product_id='+data, {  role: "page", reverse: true } );
                }
            });
    })

    //// end click btn upload
    //end upload product form
});

$(document).on('click','#btn_add_new_slot',function(){

    var slot = '  <div class="ui-grid-a">';
    slot += ' <div class="ui-block-a"><div class="ui-bar ui-bar-a" style="height:60px">';
    slot += '    <a href="#popupNested" data-rel="popup" class="ui-link ui-btn ui-shadow ui-corner-all" data-transition="pop">Chọn Hình Ảnh.</a>';
    slot += '</div></div>';
    slot += '    <div class="ui-block-b"><div class="ui-bar ui-bar-a" style="height:60px">';
    slot += '        <a href="#popupNested" data-rel="popup" class="ui-link ui-btn ui-shadow ui-corner-all" data-transition="pop">Chọn Hình Ảnh.</a>';
    slot += '    </div></div>';
    slot += '</div>';
    $('#row_images').append(slot);
});
$(document).on('click','.gel_item',function(){
    $(this).next().show();
});
$(document).on('click','.img_close', function(){
    $(this).parent().hide();
    $(this).parent().hide();
})
$(document).on('click','.img_del', function(){
    $(this).parent().parent().remove();
    $(this).parent().hide();
})
$(document).on('click','.img_set_thumnail', function(){
    var src = $(this).attr('data_rel');
    $('#images').val(src);

    $('#img_append').children('.col50').removeClass('thumb_border');
    $(this).parent().parent().addClass('thumb_border');
    $(this).parent().hide();
})
$(document).on('blur','#price', function(){
   $(this).formatCurrency($.formatCurrency.regions['vi-VN']);
})

